﻿using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChineseSchool.Web
{
    public partial class UserComment : System.Web.UI.Page
    {
        B_UserComment commentService = new B_UserComment();
        B_User userService = new B_User();
        B_Insert_Update_Delete InsertUpdate = new B_Insert_Update_Delete();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                divAdd.Visible = false;
                divContent.Visible = true;
                BindGrid();
            }
        }

        private void BindGrid()
        {
            string userName = Session["Username"].ToString();
            string userPassword = Session["PassWord"].ToString();
            var currentUser = userService.Selectby_username_password(userName, userPassword);
            if (currentUser != null)
            {
                var lstLesson = commentService.GetAllsByUserId(currentUser.UserID);

                grdComment.DataSource = lstLesson;
                grdComment.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var userId = "";
            var userComment = new M_UserComment();
            userComment.PostName = Server.HtmlEncode(txtPostName.Text.Trim());
            userComment.PostContent = Server.HtmlEncode(txtPostContent.Text.Trim());
            userComment.UserName = userId;
            userComment.PostDate = DateTime.Now;

            InsertUpdate.insert("UserComment", "PostId", userComment);
            divAdd.Visible = false;
            divContent.Visible = true;
            BindGrid();
        }

        #region Grid command

        protected void GrdLessonPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdComment.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void GrdLessonRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("View", StringComparison.OrdinalIgnoreCase))
            {
                
            }
        }

        protected void GrdLessonRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BindGrid();
        }

        protected void GrdLessonRowEditing(object sender, GridViewEditEventArgs e)
        {
            grdComment.EditIndex = e.NewEditIndex;
            BindGrid();
        }

        #endregion        

        protected void BtnAddnewClick(object sender, EventArgs e)
        {
            txtPostName.Text = string.Empty;
            txtPostContent.Text = string.Empty;

            divAdd.Visible = true;
            divContent.Visible = false;
        }
    }
}